//
//  BudgetPieSectionView.m
//  BaseProject
//
//  Created by mac on 2020/10/27.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BudgetPieSectionView.h"
#import "BudgetPieItemView.h"
@interface BudgetPieSectionView ()
@property(nonatomic,strong)UILabel *nameLab;
@property(nonatomic,strong)NSMutableArray<BudgetPieItemView *> *itemsArray;
@end

@implementation BudgetPieSectionView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)createProperty
{
    self.backgroundColor = [UIColor whiteColor];
}
- (void)createUI
{
    
}
#pragma mark - setter
- (void)setIconModel:(BudgetPieItemModel *)iconModel
{
    _iconModel = iconModel;
    WEAKSELF
    self.nameLab.text = iconModel.title;
    [self.itemsArray enumerateObjectsUsingBlock:^(BudgetPieItemView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.itemsArray removeAllObjects];
    CGFloat orinY = 30*ADAPTER_WIDTH,itemHeight = 50*ADAPTER_WIDTH;
    for (NSInteger index = 0; index < iconModel.dataSource.count; index ++) {
        BudgetPieItemView *view = [[BudgetPieItemView alloc]initWithFrame:CGRectMake(0, orinY, self.width, itemHeight)];
        [self addSubview:view];
        
        orinY = view.bottom;
        if (index == (iconModel.dataSource.count - 1)) {
            iconModel.dataSource[index].showLine = NO;
        } else {
            iconModel.dataSource[index].showLine = YES;
        }
        view.iconModel = iconModel.dataSource[index];
        
        [view setRealTimeCallBackBlock:^{
            [weakSelf realTimeCallBack];
        }];
        
        [self.itemsArray addObject:view];
    }
    self.height = orinY;
}
- (void)refreshUI
{
    CGFloat orinY = 30*ADAPTER_WIDTH;
    for (NSInteger index = 0; index < self.itemsArray.count; index ++) {
        BudgetPieItemView *view = self.itemsArray[index];
        if (view.iconModel.isSelected) {
            [self addSubview:view];
            view.top = orinY;
            orinY = view.bottom;
            
            view.iconModel = view.iconModel;
        } else {
            [view removeFromSuperview];
        }
    }
    self.height = orinY;
}
#pragma mark - 回调
- (void)realTimeCallBack
{
    !self.realTimeCallBackBlock ?: self.realTimeCallBackBlock ();
}
#pragma mark - lazy懒加载
- (UILabel *)nameLab
{
    if (!_nameLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = RGB(102, 102, 102);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(16*ADAPTER_WIDTH, 15*ADAPTER_WIDTH, self.width - 32*ADAPTER_WIDTH, 13*ADAPTER_WIDTH);
        _nameLab = lab;
    }
    return _nameLab;
}
- (NSMutableArray<BudgetPieItemView *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
@end
